// miniprogram/pages/fixData/fix.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        index:0,
        index1:0,
        array:[0,10,30,50,70]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getRole()
    },
    getRole() {
        this.setData({
            roleloading: true
        })
        wx.cloud.callFunction({
            name: 'getRole',
            success: res => {
                console.log(res)
                this.setData({
                    role: res.result.data.sort((a, b) => {
                        return a.price - b.price
                    })
                })
            },
            fail: err => {
                wx.showToast({
                    icon: 'none',
                    title: '调用失败',
                })
                console.error('[云函数] [login] 调用失败：', err)
            }
        })
    },
    showModal(e) {
        let data = this.data.role[e.currentTarget.dataset.index];
        console.log(data)
        this.setData({
            skill:''
        })
        this.setData({
            modalName: e.currentTarget.dataset.target,
            curName: e.currentTarget.dataset.name,
            curId: e.currentTarget.dataset.id,
            aoe: data.aoe,
            control: data.control,
            index: this.data.array.indexOf(data.aoe),
            index1: this.data.array.indexOf(data.control),
            blood_1: data.attribute ? data.attribute[0].blood : '',
            armor_1: data.attribute ? data.attribute[0].armor : '',
            aspeed_1: data.attribute ? data.attribute[0].aspeed : '',
            max_attack_1: data.attribute ? data.attribute[0].max_attack : '',
            min_attack_1: data.attribute ? data.attribute[0].min_attack : '',
            blood_2: data.attribute ? data.attribute[1].blood : '',
            armor_2: data.attribute ? data.attribute[1].armor : '',
            aspeed_2: data.attribute ? data.attribute[1].aspeed : '',
            max_attack_2: data.attribute ? data.attribute[1].max_attack : '',
            min_attack_2: data.attribute ? data.attribute[1].min_attack : '',
            blood_3: data.attribute ? data.attribute[2].blood: '',
            armor_3: data.attribute ? data.attribute[2].armor: '',
            aspeed_3: data.attribute ? data.attribute[2].aspeed: '',
            max_attack_3: data.attribute ? data.attribute[2].max_attack: '',
            min_attack_3: data.attribute ? data.attribute[2].min_attack: '',
            attDistance: data.attDistance,
            skill: data.skill
        })
        
    },
    hideModal(e) {
        this.setData({
            modalName: null
        })
    },
    bindPickerChange(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            index: e.detail.value
        })
    },
    bindPickerChange1(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            index1: e.detail.value
        })
    },
    submit(e){
        console.log(e)
        let data = e.detail.value;
        data._id = this.data.curId;
        data.aoe = this.data.array[this.data.index];
        data.control = this.data.array[this.data.index1];
        console.log(data)
        wx.cloud.callFunction({
            name: 'fix',
            data: data,
            success: res => {
                console.log(res)
                wx.showToast({
                    title: '成功',
                })
                this.hideModal()
                this.getRole()
            },
            fail: err => {
                wx.showToast({
                    icon: 'none',
                    title: '调用失败',
                })
                console.error('[云函数] [login] 调用失败：', err)
            }
        })
    }
})